list comprehension
MetPy Mondays `#34 - List Comprehensions : Unidata Developer's Blog
https://www.unidata.ucar.edu/blogs/developer/en/entry/metpy-mondays-34-list-comprehensions
code:python
i if i%2==0 and i>200 else 0 for i in y2
code:python
dada=1,2,3],4,5,6,[7,8,9
flattened=a for b in dada for a in b
#flattened= []
#for b in data:
# for a in b:
# flattened.append(a)
PythonSpeed/PerformanceTips - Python Wiki
Besides the syntactic benefit of list comprehensions, they are often as fast or faster than equivalent use of map operator.
They provide a syntactically more compact and more efficient way of writing the above for loop:
code:python
newlist = s.upper() for s in oldlist
instead of
code:python
newlist = []
for word in oldlist:
newlist.append(word.upper())
filter
code:python
values = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
even = x for x in values if x % 2 == 0
print(even)
2, 4, 6, 8, 10
Totorials
Python List Comprehensions: Explained Visually - Trey Hunner
(Tutorial) Python List Comprehension - DataCamp
https://www.datacamp.com/community/tutorials/python-list-comprehension
Tips
parallel processing - How to parallelize list-comprehension calculations in Python? - Stack Overflow
Dask
code:python
import dask
dtemp=[dask.delayed(f)n for n in range(M)]
dask.compute(*dtemp)
Working with Collections — Dask
Do not overuse it
Overusing list comprehensions and generator expressions in Python - Trey Hunner
Why You Should Not Overuse List Comprehensions in Python - KDnuggets